﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace FAST
{
    public partial class Conflicts : Form
    {

        private List<FilesHandler> leftList = new List<FilesHandler>();
        private List<FilesHandler> rightList = new List<FilesHandler>();
        private List<int[]> l_con = new List<int[]>();
        private List<int[]> r_con = new List<int[]>();

        private string[] conflictType = new string[] { "L modifed, R delete", "R modifed, L delete", "L and R modified" };
        private string[] latestFileAt = new string[] { "Left", "Right" };

        private const int DEFAULT = 0;
        private const int COPY_LEFT = 1;
        private const int COPY_RIGHT = 2;
        private const int DELETE = 3;

        private ConflictsCore core = null;


        public Conflicts()
        {
            InitializeComponent();
            this.MouseHover += new EventHandler(this.form_MouseHover);
            this.hoverPanel.MouseHover += new EventHandler(this.hoverPanel_MouseHover);
            this.dgdConflicts.MouseHover += new EventHandler(this.form_MouseHover);
        }

        public Conflicts(List<FilesHandler> l_list, List<FilesHandler> r_list, List<int[]> l_con, List<int[]> r_con/*, string leftFoldPath, string rightFoldPath*/)
        {
            this.r_con = r_con;
            this.l_con = l_con;
            this.leftList = l_list;
            this.rightList = r_list;
            core = new ConflictsCore(leftList, rightList, l_con, r_con);
            InitializeComponent();
            this.MouseHover += new EventHandler(this.form_MouseHover);
            this.hoverPanel.MouseHover += new EventHandler(this.hoverPanel_MouseHover);
            this.dgdConflicts.MouseHover += new EventHandler(this.form_MouseHover);
        }

        public void form_MouseHover(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;
        }

        public void hoverPanel_MouseHover(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.Sizable;
        }

        private void Conflicts_Load(object sender, EventArgs e)
        {
            dgdConflicts.DataSource = core.createConflictList();
        }

        public void closeButton_Click(Object sender)
        {
            int rowCount = dgdConflicts.RowCount;
            int count = 0;
            
        
            while (count < rowCount)
            {
                 core.processConflicts(count, DEFAULT);
            }
            this.Dispose();

        }
        
        private void btnOK_Click(object sender, EventArgs e)
        {
            int rowCount = dgdConflicts.RowCount;
            int count = 0;
            bool error = false;
            DataGridViewCheckBoxCell iLtoRCell, iRtoLCell, iNthCell;
                       
            while (count < rowCount)
            {
                //Setting of the datagrid
                iLtoRCell = dgdConflicts.Rows[count].Cells["iLtoRCopy"] as DataGridViewCheckBoxCell;
                iRtoLCell = dgdConflicts.Rows[count].Cells["iRtoLCopy"] as DataGridViewCheckBoxCell;
                iNthCell = dgdConflicts.Rows[count].Cells["iNth"] as DataGridViewCheckBoxCell;

                if (iLtoRCell != null && iLtoRCell.Value != null && (bool)iLtoRCell.Value == true)
                {
                    core.processConflicts(count,COPY_RIGHT);
                }
                else if (iRtoLCell != null && iRtoLCell.Value != null && (bool)iRtoLCell.Value == true)
                {
                    core.processConflicts(count,COPY_LEFT);
                }
                else if (iNthCell != null && iNthCell.Value != null && (bool)iNthCell.Value == true)
                {
                    core.processConflicts(count, DEFAULT);
                }
                else
                {
                    error = true;
                    MessageBox.Show("Please select the required action!");
                }
              
                count = count + 1;
            }
            if(error == false)
                this.Dispose();
        }
        public void dgdConflicts_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int selectedColIndex = e.ColumnIndex;
            int rowCount = dgdConflicts.RowCount;
            int count = 0;
            if (selectedColIndex == dgdConflicts.Columns["iLtoRCopy"].Index)
            {
                while (count < rowCount)
                {
                    dgdConflicts.Rows[count].Cells[selectedColIndex].Value = false;
                }
            }
        }
        private void dgdConflicts_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //find the selected column
            int selectedColIndex = e.ColumnIndex;
            int select = e.RowIndex;

            //Store all available columns
            int[] columnIndex = { dgdConflicts.Columns["iLtoRCopy"].Index, 
                                  dgdConflicts.Columns["iRtoLCopy"].Index, 
                                  dgdConflicts.Columns["iNth"].Index };
            //Check where is selected
             if ((selectedColIndex == dgdConflicts.Columns["iLtoRCopy"].Index ||
                 selectedColIndex == dgdConflicts.Columns["iRtoLCopy"].Index ||
                 selectedColIndex == dgdConflicts.Columns["iNth"].Index))
            {
                for(int i = 0; i< columnIndex.Count(); i++)
                {
                    if(selectedColIndex != columnIndex[i])
                        if(e.RowIndex != -1)
                        dgdConflicts.Rows[e.RowIndex].Cells[columnIndex[i]].Value = false;
                }
                

            }

        }


    }
}
